package com.wyt.lzzy;

public class Bedroom {
    private String name;
    private int id;
    private int age;

    public Bedroom(String name, int id, int age){
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        Bedroom[] arr = new Bedroom[3];
        Bedroom bed1 = new Bedroom("xiaotao",1,20);
        Bedroom bed2 = new Bedroom("xiaozhang",2,19);

        arr[0] = bed1;
        arr[1] = bed2;

        Bedroom bed3 = new Bedroom("xiaomo",3,21);

        boolean flag = contains(arr, bed3.getId());
        if(flag){
            System.out.println("当前id重复，请修改id后再进行添加");
        }
        else{
            int count = getCount(arr);
            if(count == arr.length){
                Bedroom[] newArr = creatNewArr(arr);
                newArr[count] = bed3;
                printArr(newArr);
            }
            else{
                arr[count] = bed3;
                printArr(arr);
            }
        }
    }

    public static void printArr(Bedroom[] arr) {
        for (int i = 0; i < arr.length; i++){
            Bedroom bed = arr[i];
            if(bed != null){
                System.out.println(bed.getId() + ", " + bed.getName() + ", " + bed.getAge());
            }
        }
    }

    public static Bedroom[] creatNewArr(Bedroom[] arr){
        Bedroom[] newArr = new Bedroom[arr.length + 1];
        for (int i = 0; i < arr.length; i++){
            newArr[i] = arr[i];
        }
        return newArr;
    }
    public static int getCount(Bedroom[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++){
            if(arr[i] != null){
                count++;
            }
        }
        return count;
    }

    public static boolean contains(Bedroom[] arr, int id){
        for (int i = 0; i < arr.length; i++){
            Bedroom bed = arr[i];
            if(bed != null){
                int bid = bed.getId();
                if(bid == id){
                    return true;
                }
            }
        }
        return false;
    }

}
